package com.zbs.framework.common.enums;

import com.zbs.framework.common.enums.annotations.EnumProperty;
import org.reflections.Reflections;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class BaseEnumUtil {
    public static Map<String, String> getBaseEnumCode(Class<?>... clazz) {
        Object[] objects = Arrays.stream(clazz).map(Class::getName).collect(Collectors.toList()).toArray(new Object[]{});
        Reflections reflections = new Reflections(objects);

        Set<Class<? extends BaseEnum>> monitorClasses = reflections.getSubTypesOf(BaseEnum.class);
        Map<String, String> map = new HashMap<>();
        monitorClasses.forEach(m -> {
            try {
                BaseEnum[] enumConstants = m.getEnumConstants();
                for (BaseEnum anEnum : enumConstants) {
                    Arrays.stream(anEnum.getClass().getFields()).forEach(field -> {
                        EnumProperty enumProperty = field.getAnnotation(EnumProperty.class);
                        map.put(enumProperty == null ? anEnum.getCode().toString() : enumProperty.value(), anEnum.getCode().toString());
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return map;
    }
}
